package org.lanqiao.tjut.enity;

import java.util.List;
import java.util.Scanner;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.lanqiao.tjut.db.DBDriverFactory;
import org.lanqiao.tjut.edu.EduServer;

public class Teacher {
	Print pl = new Print();

	/**
	 * 教师功能菜单
	 */
	public void logTeacher() {
		EduServer edu = new EduServer();
		Scanner input = new Scanner(System.in);
		System.out.println("=========身份::老师=========");
		System.out.println("==========================");
		System.out.println("*      1.添加学生信息                    *");
		System.out.println("*      2.查询学生信息                    *");
		System.out.println("*      3.修改学生信息                    *");
		System.out.println("*      4.删除学生信息                    *");
		System.out.println("*      0.返回                                  *");
		System.out.println("==========================");
		System.out.println("请选择功能菜单：");
		int userSel = input.nextInt();
		switch (userSel) {
		case 1:
			// 添加学生信息
			insertStu();
			break;
		case 2:
			// 查询学生信息
			queryMenu();
			break;
		case 3:
			// 修改学生信息
			updateStu();
			break;
		case 4:
			// 删除学生信息
			deleteStu();
			break;
		case 0:
		default:
			// 返回主登录界面
			edu.Login();
			break;
		}
	}

	/**
	 * 教师登录验证，输入账号密码
	 */
	public void entryTeacher() {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入账号：");
		String accountNum = input.next();
		System.out.println("请输入密码：");
		String passwordNum = input.next();
		if (accountNum.equals("sys") && passwordNum.equals("123456")) {
			logTeacher();
		} else {
			System.out.println("账号或密码输入错误，请重新输入！");
			entryTeacher();
		}
	}

	/**
	 * 教师菜单1.添加学生信息
	 */
	public void insertStu() {
		Scanner sc = new Scanner(System.in);
		while (true) {
			System.out.println("录入学生学号：");
			int instuid = sc.nextInt();
			// 检验该学生是否已经输入
			boolean isExist = isStudentexisted(instuid);
			if (isExist) {
				System.out.println("该学生信息已经存在!\n请重新输入信息！");
				//重新录入信息
				insertStu();
			} else {
				//录入信息
				System.out.println("请输入学生初始密码：");
				String strStupword = sc.next();
				System.out.println("请输入学生姓名:");
				String strStuname = sc.next();
				System.out.println("请输入学生性别:");
				String strStuSex = sc.next();
				System.out.println("请输入学生年龄:");
				int inStuage = sc.nextInt();
				System.out.println("请输入学生联系方式:");
				String inStutelnum = sc.next();
				System.out.println("请输入学生地址:");
				String strStuaddress = sc.next();
				
				String strSql = "INSERT INTO EDUSTUDENT(STUPSW,STUNAME,STUID,STUSEX,STUAGE,STUPHONE,STUADDRESS) "
						+ "VALUES(?,?,?,?,?,?,?)";
				int i = DBDriverFactory.getDBDriverInstance().update(strSql, new Object[] { strStupword, strStuname,
						instuid, strStuSex, inStuage, inStutelnum, strStuaddress});
				if (i > 0) {
					System.out.println("该学生信息录入成功！");
				} else {
					System.out.println("该学生信息录入失败！");
				}
				String strSql1 = "SELECT STUID,STUNAME,STUPSW,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE ISDELETED = 'N' ORDER BY STUCODE DESC";
				List lst = DBDriverFactory.getDBDriverInstance().query(strSql1, new MapListHandler());
				pl.printList(lst);
			}
			System.out.println("是否继续录入其他学生的信息？(1-继续*0-返回)");
			int in = sc.nextInt();
			if (in==0) {
				break;
			}
		}
		// 返回教师功能菜单
		logTeacher();
	}

	/**
	 * 查询功能菜单列表
	 */
	public void queryMenu() {
		Scanner sc = new Scanner(System.in);
		System.out.println("=====================");
		System.out.println("1.查询所有学生信息");
		System.out.println("2.根据学号查询单个学生信息");
		System.out.println("3.根据姓名模糊查询学生信息");
		System.out.println("4.根据地址模糊查询学生信息");
		System.out.println("5.根据年龄段查询学生信息");
		System.out.println("0.返回");
		System.out.println("=====================");
		int inIput = sc.nextInt();
		switch (inIput) {
		case 1:
			// 查询所有学生信息
			queryAllStu();
			break;
		case 2:
			// 根据指定编号(学号)查询单个学生信息
			queryOneStu();
			break;
		case 3:
			// 根据姓名模糊查询学生信息
			queryNameStu();
			break;
		case 4:
			// 根据地址模糊查询学生信息
			queryAdreStu();
			break;
		case 5:
			// 根据年龄段查询学生信息
			queryAgeStu();
			break;
		case 0:
			logTeacher();
			break;
		default:
			System.out.println("输入错误，请重新输入！");
			queryMenu();
		}
	}

	/**
	 * 1.查询所有学生信息
	 */
	public void queryAllStu() {
		System.out.println("**查询所有学生信息**");
		Scanner sc = new Scanner(System.in);
		String strSql = "SELECT STUID,STUNAME,STUPSW,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE ISDELETED = 'N' ORDER BY STUCODE DESC";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
		pl.printList(lst);
		// 返回上层菜单
		System.out.println("0.返回");
		int in = sc.nextInt();
		if (in==0) {
			queryMenu();
		}
	}

	/**
	 * 2.根据学号查询单个学生信息
	 */
	public void queryOneStu() {
		System.out.println("**根据学号查询单个学生信息**");
		Scanner sc = new Scanner(System.in);
			System.out.println("请输入学生学号：");
			int inStuid = sc.nextInt();
			String strSql = "SELECT STUID,STUPSW,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE ISDELETED ='N' AND STUID = ?";
			List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler(),
					new Object[] { inStuid });
			pl.printList(lst);
			System.out.println("是否继续查询单个学生信息？(1-继续*0-返回)");
			int in = sc.nextInt();
			if (in==0) {
				queryMenu();
			} else {
				queryOneStu();
			}
		}
	

	/**
	 * 3.根据姓名模糊查询学生信息
	 */
	public void queryNameStu() {
		System.out.println("**根据姓名模糊查询学生信息**");
		Scanner sc = new Scanner(System.in);
			System.out.println("请输入姓名模糊查询关键字：");
			String strName = sc.next();
			strName = "%" + strName + "%";
			String strSql = "SELECT STUID,STUPSW,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE ISDELETED = 'N' AND STUNAME LIKE '" + strName + "' ORDER BY STUCODE DESC";
			List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
			pl.printList(lst);
			System.out.println("是否继续根据姓名模糊查询学生信息？(1-继续*0-返回)");
			int in = sc.nextInt();
			if (in==0) {
				queryMenu();
			} else {
				queryNameStu();
			}
		}

	/**
	 * 4.根据地址模糊查询学生信息
	 */
	public void queryAdreStu() {
		System.out.println("**根据地址模糊查询学生信息**");
		Scanner sc = new Scanner(System.in);
			System.out.println("请输入需要根据地址模糊查询的字：");
			String stAdress = sc.next();
			stAdress = "%" + stAdress + "%";
			String strSql = "SELECT STUID,STUPSW,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE ISDELETED = 'N' AND STUADDRESS LIKE '" + stAdress + "' ORDER BY STUCODE DESC";
			List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
			pl.printList(lst);
			System.out.println("是否继续根据地址模糊查询学生信息？(1-继续*0-返回)");
			int in = sc.nextInt();
			if (in==0) {
				queryMenu();
			} else {
				queryAdreStu();
			}
		}

	/**
	 * 5.根据年龄段模糊查询学生信息
	 */
	public void queryAgeStu() {
		System.out.println("**根据年龄段模糊查询学生信息**");
		Scanner sc = new Scanner(System.in);
			System.out.println("请输入较小的年龄：");
			int stAge1 = sc.nextInt();
			System.out.println("请输入较大的年龄：");
			int stAge2 = sc.nextInt();
			String strSql = "SELECT STUID,STUPSW,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE ISDELETED = 'N' AND STUAGE >=? AND STUAGE<=? ORDER BY STUCODE DESC";
			List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler(),
					new Object[] { stAge1, stAge2 });
			pl.printList(lst);
			System.out.println("是否继续根据年龄段模糊查询学生信息?(1-继续*0-返回)");
			int in = sc.nextInt();
			if (in==0) {
				queryMenu();
			} else {
				queryAgeStu();
			}
		}

	/**
	 * 修改学生信息
	 */
	public void updateStu() {
		Scanner sc = new Scanner(System.in);
		String strSql = "SELECT STUID,STUPSW,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE ISDELETED = 'N' ORDER BY STUCODE DESC";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
		pl.printList(lst);
		while (true) {
			System.out.println("请选择要修改的学生（输入学号）：");
			int instuid = sc.nextInt();
			System.out.println("请输入新的学生初始密码：");
			String strStupword = sc.next();
			System.out.println("请输入新的学生姓名:");
			String strStuname = sc.next();
			System.out.println("请输入新的学生性别:");
			String strStuSex = sc.next();
			System.out.println("请输入新的学生年龄:");
			int inStuage = sc.nextInt();
			System.out.println("请输入新的学生联系方式:");
			String inStutelnum = sc.next();
			System.out.println("请输入新的学生地址:");
			String strStuaddress = sc.next();
			System.out.println("请输入新的学生就读状态:(N:在读|Y:休学)");
			String instustate = sc.next();
			String strSql1 = "update EDUSTUDENT set STUPSW='" + strStupword + "',STUNAME='" + strStuname + "',STUID='"
					+ instuid + "',STUSEX='" + strStuSex + "',STUAGE='" + inStuage + "',STUPHONE='" + inStutelnum
					+ "',STUADDRESS='" + strStuaddress + "',ISDELETED='" + instustate + "'where STUID=?";
			int i = DBDriverFactory.getDBDriverInstance().update(strSql1, new Object[] { instuid });
			if (i > 0) {
				System.out.println("信息修改成功！");
			} else {
				System.out.println("信息修改失败！");
			}
			String strSql2 = "SELECT STUID,STUPSW,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE ISDELETED = 'N' ORDER BY STUCODE DESC";
			List lst1 = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
			pl.printList(lst1);

			System.out.println("是否继续修改其他学生的信息？(1-继续*0-返回)");
			int in = sc.nextInt();
			if (in==0) {
				logTeacher();
				break;
			}
		}

	}

	/**
	 * 删除学生信息
	 */
	public void deleteStu() {
		Scanner sc = new Scanner(System.in);
		String strSql = "SELECT STUID,STUPSW,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE ISDELETED = 'N' ORDER BY STUCODE DESC";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
		pl.printList(lst);
		while (true) {
			System.out.println("请输入您要删除的学生学号：");
			int instuid = sc.nextInt();
			String strSql1 = "UPDATE  EDUSTUDENT SET ISDELETED = 'Y' WHERE STUID=?";
			int i = DBDriverFactory.getDBDriverInstance().update(strSql1, new Object[] { instuid });
			if (i > 0) {
				System.out.println("该学生信息删除成功！");
			} else {
				System.out.println("该学生信息删除失败！");
			}
			String strSql2 = "SELECT STUID,STUPSW,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE ISDELETED = 'N' ORDER BY DESC";
			List lst1 = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
			pl.printList(lst1);
			System.out.println("是否继续修改其他学生的信息？(1-继续*0-返回)");
			int in = sc.nextInt();
			if (in==0) {
				logTeacher();
				break;
			}
		}
	}

	/**
	 * 查询验证该学号对应的学生信息是否已经存在
	 * 
	 * @param stuid
	 *            学生学号
	 * @return 查询结果
	 */
	private boolean isStudentexisted(Integer stuid) {
		// 查询语句
		String strSql = "SELECT * FROM EDUSTUDENT WHERE STUID = ?";
		// 查询结果
		List<StuInfo> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<StuInfo>(StuInfo.class), new Object[] { stuid });

		return (lst != null && lst.size() > 0) ? true : false;

	}
}
